﻿using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class UserEntityService : IEntityService<NguoiSuDung>
    {
        public List<NguoiSuDung> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var list = entities.NguoiSuDungs.Include("NhanVien").Include("NhomNguoiDung").ToList();
                return list;
            }
        }

        public bool Add(NguoiSuDung entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NguoiSuDungs.Where(x => x.MaNhanVien == entity.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    Messenger.Default.Send(new StatusMessage("Nhân viên này đã có tài khoản", 5000, StatusType.Error));
                }

                try
                {
                    if (entity.MatKhau == null) entity.MatKhau = "123456";
                    entities.NguoiSuDungs.Add(entity);
                    entities.SaveChanges();
                    return true;
                }
                catch
                {
                    Messenger.Default.Send(StatusMessage.DatabaseErrorMessage());
                }
                return false;
            }
        }

        public bool Update(NguoiSuDung entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NguoiSuDungs.Where(x => x.MaNhanVien == entity.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    query.TenDangNhap = entity.TenDangNhap;
                    query.MatKhau = entity.MatKhau;
                    query.MaNhom = entity.MaNhom;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(NguoiSuDung entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NguoiSuDungs.Where(x => x.MaNhanVien == entity.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    try
                    {
                        query.TenDangNhap = entity.TenDangNhap;
                        query.MatKhau = entity.MatKhau;
                        query.MaNhom = entity.MaNhom;
                        entities.NguoiSuDungs.Remove(query);
                        entities.SaveChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                    }
                }
                return true;
            }
        }

        public bool DeleteList(List<NguoiSuDung> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in list)
                {
                    var query = entities.NguoiSuDungs.Where(x => x.MaNhanVien == item.MaNhanVien).FirstOrDefault();
                    entities.NguoiSuDungs.Remove(query);
                }
                entities.SaveChanges();
                return true;
            }
        }

        public List<NguoiSuDung> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.NguoiSuDungs.Include("NhanVien").Include("NhomNguoiDung").OrderBy(p => p.MaNhanVien).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.NguoiSuDungs.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }
    }
}
